package com.tianqu.tms.www.member.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.webservice.weichat.api.MobileMessage;
import com.tianqu.tms.core.webservice.weichat.api.PullingExecutor;
import com.tianqu.tms.core.webservice.weichat.api.rpc.MemberUpdateAccessCardInfoBo;
import com.tianqu.tms.entity.IntegralcardUpdateHistory;
import com.tianqu.tms.entity.Member;
import com.tianqu.tms.www.member.viewhelper.MemberIntegralViewHelper;

/**
 * @author zhangxueliang
 * 积分修改结果controller
 */
public class MemberIntegralUpdateResultController extends TmsController{
	private Logger logger = Logger.getLogger(MemberIntegralUpdateResultController.class);
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			Member mem = Member.getInstance();
			MemberIntegralViewHelper viewhelper = (MemberIntegralViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
			mem.setAccessCardId(viewhelper.getAccessCardIdNew());
			mem.setAccessCardNo(viewhelper.getAccessCardNoNew());
			mem.setMemName(viewhelper.getMemberId());
			TmsUserProfile profile = uc.getUserProfile();
			mem.setUpdateUserId(profile.getUniqueId());
			mem.updateIntegralcard(mem);
			
			IntegralcardUpdateHistory history = IntegralcardUpdateHistory.getInstance();
			history = ToHistory(history,viewhelper);
			history.setInsertUserId(profile.getUniqueId());
			history.setMemberId(viewhelper.getMemberId());
			history.insertHistory(history);
			logger.debug(this.getClass().getName() + "#execute was completed.");
			controller.commit();
 //         微信同步
//			UserManager userApi = UserManagerImpl.create();
//			//会员ID、积分卡编号、积分卡卡号
//			userApi.memberUpdateAccessCardInfo(viewhelper.getMemberId(), mem.getAccessCardId(), mem.getAccessCardNo());
			MobileMessage message = new MobileMessage();
			message.putValue("MemberId", viewhelper.getMemberId());
			message.setMessage("TMS积分修改调用[会员/修改积分信息修改]接口失败,会员ID为#MemberId#");
			MemberUpdateAccessCardInfoBo api = new MemberUpdateAccessCardInfoBo();
			api.setMobileMessage(message);
			api.setMemberId(viewhelper.getMemberId());
			api.setAccessCardId(mem.getAccessCardId());
			api.setAccessCardNo(mem.getAccessCardNo());
			PullingExecutor.getInstance().submit(api);
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
		
	}
	/**
	 * view -> entity
	 * @param history
	 * @param view
	 * @return
	 */
	private IntegralcardUpdateHistory ToHistory(IntegralcardUpdateHistory history,MemberIntegralViewHelper view){
		if(StringUtil.isNullOrBlank(view.getAccessCardId())){
			history.setBusinessType(1);
			history.setAccessCardIdNew(view.getAccessCardIdNew());
			history.setAccessCardIdOld(view.getAccessCardIdNew());
			history.setAccessCardNoNew(view.getAccessCardNoNew());
			history.setAccessCardNoOld(view.getAccessCardNoNew());
		}else{
			history.setBusinessType(2);
			history.setAccessCardIdNew(view.getAccessCardIdNew());
			history.setAccessCardIdOld(view.getAccessCardId());
			history.setAccessCardNoNew(view.getAccessCardNoNew());
			history.setAccessCardNoOld(view.getAccessCardNo());
		}
		history.setMemberType(1);		
		
		if(StringUtil.isNullOrBlank(view.getChangePrice())){
			history.setChangePrice(0);
		}else{
			history.setChangePrice(Integer.parseInt(view.getChangePrice()));
		}
		
		history.setRemarks(view.getRemarks());
		
		return history;
	}

}
